package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageResult;
import com.oracle.mapper.SswsMapper;
import com.oracle.pojo.Ssws;
import com.oracle.pojo.SswsExample;
import com.oracle.service.SswsService;
import com.oracle.vo.SswsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SswsServiceImpl implements SswsService {
    @Autowired(required = false)
    private SswsMapper sswsMapper;

    @Override
    public List<Ssws> findList()
    {
        List<Ssws> ssws=sswsMapper.selectByExample(null);
        return ssws;
    }

    @Override
    public PageResult findPages(Integer pageNo, Integer pageSize, Ssws ssws)
    {
        PageHelper.startPage(pageNo,pageSize);

        SswsExample example = new SswsExample();
        SswsExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(ssws.getSswsId()))
        {
            criteria.andSswsIdEqualTo(ssws.getSswsId());
        }
        if (!ObjectUtils.isEmpty(ssws.getSsId()))
        {
            criteria.andSsIdEqualTo(ssws.getSsId());
        }
        if (!ObjectUtils.isEmpty(ssws.getSsDormbuild()))
        {
            criteria.andSsDormbuildEqualTo(ssws.getSsDormbuild());
        }
        if (!ObjectUtils.isEmpty(ssws.getSswsPf()))
        {
            criteria.andSswsPfEqualTo(ssws.getSswsPf());
        }
        if (!ObjectUtils.isEmpty(ssws.getCjTime()))
        {
            criteria.andCjTimeEqualTo(ssws.getCjTime());
        }
        if (!ObjectUtils.isEmpty(ssws.getUpTime()))
        {
            criteria.andUpTimeEqualTo(ssws.getUpTime());
        }

        example.setOrderByClause("ssws_id");

        Page<Ssws> sswsPage = (Page<Ssws>)sswsMapper.selectByExample(example);

        List<Ssws> sswsList = sswsPage.getResult();

        List<SswsVo> sswsVoList = buildGlyVoList(sswsList);

        return new PageResult(sswsPage.getTotal(),sswsVoList);
    }

    private List<SswsVo> buildGlyVoList(List<Ssws> sswsList)
    {
        List<SswsVo> sswsVoList = new ArrayList<>();
        sswsList.forEach(ssws -> {
            SswsVo sswsVo2 = new SswsVo();

            BeanUtils.copyProperties(ssws,sswsVo2);

            sswsVoList.add(sswsVo2);
        });

        return sswsVoList;
    }
}
